package com.zhuoyue.biz.core.command.impl;

import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zhuoyue.biz.core.command.Device;
import com.zhuoyue.biz.core.command.constant.BeiJingYinYueFhwise0Fe60203CmdDeal;
import com.zhuoyue.common.utils.CmdUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * <p>Title: 背景音乐器<／p>
 * <p>Description: <／p>
 * <p>Company: JiZi<／p>
 *
 * @author wjming
 * @version 1.0
 * @date 2018年12月10日
 */
public class BeiJingYinYueFhwise0Fe60203 implements Device {
    private static final String OPEN = "OPEN";
    private static final String CLOSE = "CLOSE";
    private static final String START = "START";
    private static final String STOP = "STOP";
    private static final String LAST = "LAST";
    private static final String NEXT = "NEXT";
    private static final String VOLUME_DEL = "VOLUME_DEL";
    private static final String VOLUME_ADD = "VOLUME_ADD";

    @Override
    public String getCommand(String controlParam) {

        Map<String, String> map = (Map) JSONObject.parse(controlParam);

        String controlType = map.get("controlType");
        if (StringUtils.isEmpty(controlType)) {
            return null;
        }

        String cmd = "";
        if (BeiJingYinYueFhwise0Fe60203CmdDeal.ControlType.GET_ID.equals(controlType)) {
            cmd = getIdCmd(map);
        } else if (BeiJingYinYueFhwise0Fe60203CmdDeal.ControlType.CONTROL.equals(controlType)) {
            cmd = getControlCmd(map);
        }

        return cmd;

    }


    private String getControlCmd(Map<String, String> map) {

        String gatewayMac = map.get("gatewayMac");
        String deviceMac = map.get("deviceMac");
        String fhwiseId = map.get("fhwiseId");

        Map<String, String> actionsMap = (Map) JSONObject.parse(JSON.toJSONString(map.get("actions")));

        //state：  开机：OPEN  关机：CLOSE  开始播放：START  暂停：STOP  上一首：LAST  下一首：NEXT  音量-：VOLUME_DEL  音量+：VOLUME_ADD
        String state = actionsMap.get("state");

        String controlData = "";
        if (OPEN.equals(state)) {

            controlData = BeiJingYinYueFhwise0Fe60203CmdDeal.ControlCmd.CMD_4_OPEN;

        } else if (CLOSE.equals(state)) {

            controlData = BeiJingYinYueFhwise0Fe60203CmdDeal.ControlCmd.CMD_4_CLOSE;

        } else if (START.equals(state)) {

            controlData = BeiJingYinYueFhwise0Fe60203CmdDeal.ControlCmd.CMD_4_START;

        } else if (STOP.equals(state)) {

            controlData = BeiJingYinYueFhwise0Fe60203CmdDeal.ControlCmd.CMD_4_STOP;

        } else if (LAST.equals(state)) {

            controlData = BeiJingYinYueFhwise0Fe60203CmdDeal.ControlCmd.CMD_4_LAST;

        } else if (NEXT.equals(state)) {

            controlData = BeiJingYinYueFhwise0Fe60203CmdDeal.ControlCmd.CMD_4_NEXT;

        } else if (VOLUME_DEL.equals(state)) {

            controlData = BeiJingYinYueFhwise0Fe60203CmdDeal.ControlCmd.CMD_4_VOLUME_DEL;

        } else if (VOLUME_ADD.equals(state)) {

            controlData = BeiJingYinYueFhwise0Fe60203CmdDeal.ControlCmd.CMD_4_VOLUME_ADD;

        }

        String data = fhwiseId + BeiJingYinYueFhwise0Fe60203CmdDeal.ControlCmd.CMD_3 + controlData;

        String sum = CmdUtils.getCmdBodySum(data);

        //获取背景音乐协议的完成控制指令
        String controlCmd = BeiJingYinYueFhwise0Fe60203CmdDeal.ControlCmd.CMD_1 +
                data +
                sum +
                BeiJingYinYueFhwise0Fe60203CmdDeal.ControlCmd.CMD_6;


        String cmd3To8 = BeiJingYinYueFhwise0Fe60203CmdDeal.CommonCmd.CMD_3 +
                BeiJingYinYueFhwise0Fe60203CmdDeal.CommonCmd.CMD_4 +
                gatewayMac +
                deviceMac +
                CmdUtils.getDateLength(controlCmd) +
                controlCmd;

        String cmd2 = CmdUtils.getDateLength(cmd3To8);

        String cmd9 = CmdUtils.getCmdBodySum(cmd3To8);

        String fullCmd = BeiJingYinYueFhwise0Fe60203CmdDeal.CommonCmd.CMD_1 +
                cmd2 +
                cmd3To8 +
                cmd9 +
                BeiJingYinYueFhwise0Fe60203CmdDeal.CommonCmd.CMD_10;

        return fullCmd;

    }


    private String getIdCmd(Map<String, String> map) {

        String gatewayMac = map.get("gatewayMac");
        String deviceMac = map.get("deviceMac");

        String cmd3To8 = BeiJingYinYueFhwise0Fe60203CmdDeal.CommonCmd.CMD_3 +
                BeiJingYinYueFhwise0Fe60203CmdDeal.CommonCmd.CMD_4 +
                gatewayMac +
                deviceMac +
                CmdUtils.getDateLength(BeiJingYinYueFhwise0Fe60203CmdDeal.GetIdCmd.CMD_1) +
                BeiJingYinYueFhwise0Fe60203CmdDeal.GetIdCmd.CMD_1;

        String cmd2 = CmdUtils.getDateLength(cmd3To8);

        String cmd9 = CmdUtils.getCmdBodySum(cmd3To8);

        String fullCmd = BeiJingYinYueFhwise0Fe60203CmdDeal.CommonCmd.CMD_1 +
                cmd2 +
                cmd3To8 +
                cmd9 +
                BeiJingYinYueFhwise0Fe60203CmdDeal.CommonCmd.CMD_10;

        return fullCmd;
    }


}
